package com.damai.core;


import android.util.Log;

/**
 * 任务投递
 * @author Randy
 *
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class DefaultJobDelivery<T extends JobImpl> implements JobListener<T> {
	
	
	protected IJobLife life;
	public DefaultJobDelivery(IJobLife life){
		this.life = life;
	}
	
	@Override
	public void onJobSuccess(T job) {
		OnJobSuccessListener listener = job.onJobSuccessListener;
		if(listener!=null){
			try{
				listener.onJobSuccess(job);
			}catch(Throwable t){
                Log.e("JOB",t.getMessage());
			}
		}
		life.onRemoveJob(job);
	}

	@Override
	public void onJobProgress(T job) {
		OnJobProgressListener listener = job.onJobProgressListener;
		if(listener!=null){
		    try{
                listener.onJobProgress(job);
            }catch(Throwable t){
                Log.e("JOB",t.getMessage());
            }

		}
		
	}

	@Override
	public boolean onJobError(T job) {
		OnJobErrorListener listener = job.onJobErrorListener;
		if(listener!=null){
		    try{
                listener.onJobError(job);
            }catch (Throwable t){
                Log.e("JOB",t.getMessage());
            }

		}
		life.onRemoveJob(job);
		return true;
	}
	
	

}
